<?php

namespace App\Model;

use Trois\Lib\Help;

class ShopModel extends DepModel
{

    protected string $tableNameAs = 'shop';

    public function handleAllCache(int $group_id): array
    {
        $all = $this
            ->field('shop.dept_id as id,shop.dept_name as name,shop.biz_id,shop.status,shop.province,shop.city,shop.area
            ,area_1.area_name as province_name
            ,area_2.area_name as city_name
            ,area_3.area_name as area_name')
            ->alias('shop')
            ->join('by_area as area_1', 'area_1.area_sn = shop.province')
            ->join('by_area as area_2', 'area_2.area_sn = shop.city')
            ->join('by_area as area_3', 'area_3.area_sn = shop.area')
            ->where([
                'shop.group_id' => $group_id,
                'shop.is_store' => 1
            ])
            ->order('shop.px', 'ASC')
            ->all();

        $all = $all ? $all->toArray(false, false) : [];

        Help::redisPoolStringInvoke('all_cache:' . $group_id . ':' . $this->tableNameAs, json_encode($all, JSON_UNESCAPED_UNICODE), 0);

        return $all;

    }


    public function getAllCache(int $group_id): array
    {
        $all = Help::redisPoolStringInvoke('all_cache:' . $group_id . ':' . $this->tableNameAs);

        $all = $all ? json_decode($all, true) : [];

        if (!$all) {
            $all = $this->handleAllCache($group_id);
        }

        return $all ?: [];
    }

}